package com.jiudengnile.liushu.biz.bo.impl;

import java.util.ArrayList;
import java.util.List;

import com.jiudengnile.liushu.biz.bo.PermissionBO;
import com.jiudengnile.liushu.biz.dal.dao.BaseUserDAO;
import com.jiudengnile.liushu.biz.dal.dao.PermissionDAO;
import com.jiudengnile.liushu.biz.dal.dao.PermissionUserDAO;
import com.jiudengnile.liushu.biz.domains.BaseUserDO;
import com.jiudengnile.liushu.biz.domains.permission.PermissionDO;
import com.jiudengnile.liushu.biz.domains.permission.PermissionUserDO;
import com.jiudengnile.liushu.biz.query.ListWithTotalCount;
import com.jiudengnile.liushu.biz.query.PermissionQuery;

public class PermissionBOImpl implements PermissionBO {

	private PermissionDAO permissionDAO;
	
	private PermissionUserDAO permissionUserDAO;
	
	private BaseUserDAO baseUserDAO;
	
	@Override
	public long createPermissionUser(PermissionUserDO permissionUserDO) {
		return permissionUserDAO.create(permissionUserDO);
	}

	@Override
	public PermissionDO getPermissionById(long id) {
		return permissionDAO.queryById(id);
	}

	public ListWithTotalCount<PermissionDO> getPermissionForPage(PermissionQuery permissionQuery) {
		return permissionDAO.queryForPage(permissionQuery);
	}
	
	@Override
	public ListWithTotalCount<BaseUserDO> getUserByPermission(PermissionQuery permissionQuery) {
		ListWithTotalCount<PermissionUserDO> permissionUserDOs =  permissionUserDAO.queryByPemissionForPage(permissionQuery);
		if (permissionUserDOs.isEmpty()) {
			ListWithTotalCount<BaseUserDO> ret = new ListWithTotalCount<BaseUserDO>(0);
			ret.setTotalCount(permissionUserDOs.getTotalCount());
			return ret;
		}
		List<Long> ids = new ArrayList<Long>(permissionUserDOs.size());
		for (PermissionUserDO permissionUserDO : permissionUserDOs) {
			ids.add(permissionUserDO.getUserId());
		}
		List<BaseUserDO> baseUsers = baseUserDAO.queryByIds(ids);
		ListWithTotalCount<BaseUserDO> ret = new ListWithTotalCount<BaseUserDO>(baseUsers);
		ret.setTotalCount(permissionUserDOs.getTotalCount());
		return ret;
	}

	@Override
	public ListWithTotalCount<PermissionDO> getPermissionByUser(PermissionQuery permissionQuery) {
		ListWithTotalCount<PermissionUserDO> permissionUserDOs =  permissionUserDAO.queryByUserForPage(permissionQuery);
		if (permissionUserDOs.isEmpty()) {
			ListWithTotalCount<PermissionDO> ret = new ListWithTotalCount<PermissionDO>(0);
			ret.setTotalCount(permissionUserDOs.getTotalCount());
			return ret;
		}
		List<Long> ids = new ArrayList<Long>(permissionUserDOs.size());
		for (PermissionUserDO permissionUserDO : permissionUserDOs) {
			ids.add(permissionUserDO.getPermissionId());
		}
		List<PermissionDO> permissions = permissionDAO.queryByIds(ids);
		ListWithTotalCount<PermissionDO> ret = new ListWithTotalCount<PermissionDO>(permissions);
		ret.setTotalCount(permissionUserDOs.getTotalCount());
		return ret;
	}

	@Override
	public boolean hasPermission(long userId, String permissionCode) {
		if (userId == 0) {
			return false;
		}
		PermissionUserDO permissionUserDO = permissionUserDAO.queryByUserAndCode(userId, permissionCode);
		return permissionUserDO != null;
	}
	
	public long createPermission(PermissionDO permissionDO) {
		return permissionDAO.create(permissionDO);
	}

	@Override
	public PermissionDO getPermissionByCode(String code) {
		return permissionDAO.queryByCode(code);
	}

	public void setPermissionUserDAO(PermissionUserDAO permissionUserDAO) {
		this.permissionUserDAO = permissionUserDAO;
	}

	public void setPermissionDAO(PermissionDAO permissionDAO) {
		this.permissionDAO = permissionDAO;
	}

	public void setBaseUserDAO(BaseUserDAO baseUserDAO) {
		this.baseUserDAO = baseUserDAO;
	}


}
